﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class FIM_PaymentCheques : System.Web.UI.UserControl
    {
        IBLL_FIM_PaymentCheques pc = new B_FIM_PaymentCheques();
        IBLL_FIM_Cheques cheque = new B_FIM_Cheques();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LoadRadGrid1()
        {
            string strPaySeq = lblPaySeq.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            RadGrid1.DataSource = pc.GetPaymentCheques(strPaySeq);
            RadGrid1.DataBind();
        }

        public void PaymentCheques_Set(string strPaySeq, string strBgmSourceSeq, int nHeight)
        {
            if (strPaySeq == null || strPaySeq.Length == 0)
                strPaySeq = "-1";

            lblPaySeq.Text = strPaySeq;
            lblBgmSourceSeq.Text = strBgmSourceSeq;
            LoadRadGrid1();
            RadGrid1.Height = Unit.Pixel(nHeight);

            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                GridDataItem item = RadGrid1.Items[i];
                if (item["pay_seq"].Text == strPaySeq)
                {
                    item.Selected = true;
                    ((Label)RadGrid1.Items[i].FindControl("lblAmount")).Visible = false;
                    ((RadNumericTextBox)RadGrid1.Items[i].FindControl("txtAmount")).Visible = true;
                }
                else
                {
                    item.Selected = false;
                }
            }

            txtAmount_TextChanged(null, null);
        }

        protected void SetSumAmount()
        {
            double dSumAmount = 0;
            string strAmount;

            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (RadGrid1.Items[i].Selected)
                {
                    strAmount = ((RadNumericTextBox)RadGrid1.Items[i].FindControl("txtAmount")).Text;

                    if (strAmount.Length > 0)
                        dSumAmount = dSumAmount + Convert.ToDouble(strAmount);
                }
            }

            lblSumAmount.Text = dSumAmount.ToString();
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick")
            {
                bool bSelect = false;
                for (int i = 0; i < RadGrid1.Items.Count; i++)
                {
                    bSelect = RadGrid1.Items[i].Selected;
                    ((Label)RadGrid1.Items[i].FindControl("lblAmount")).Visible = !bSelect;
                    ((RadNumericTextBox)RadGrid1.Items[i].FindControl("txtAmount")).Visible = bSelect;
                }

                SetSumAmount();
            }
        }

        protected void txtAmount_TextChanged(object sender, EventArgs e)
        {
            SetSumAmount();
        }
    }
}